package cn.e3mall.order.controller;

import cn.e3.commom.utils.E3Result;
import cn.e3mall.cart.service.CartService;
import cn.e3mall.order.pojo.OrderInfo;
import cn.e3mall.order.service.OrderService;
import cn.e3mall.pojo.TbItem;
import cn.e3mall.pojo.TbUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class OrderController {

    @Autowired
    private CartService cartService;
    @Autowired
    private OrderService orderService;

    @RequestMapping("/order/order-cart")
    public String showOrderCart(Model model){

        List<TbItem> cartList = cartService.getCartList(5);
        model.addAttribute("cartList",cartList);
        return "order-cart";
    }

    @RequestMapping("/order/create")
    public String createOrder(OrderInfo orderInfo, HttpServletRequest request){

        TbUser user = (TbUser) request.getAttribute("user");
        // userId
        orderInfo.setUserId(user.getId());
        // buyerNick
        orderInfo.setBuyerNick(user.getUsername());

        E3Result e3Result = orderService.createOrder(orderInfo);
        if (e3Result.getStatus()  == 200){
            cartService.cleanCartItem(user.getId());
        }

        //返回页面所需要的数据
        request.setAttribute("orderId",e3Result.getData());
        request.setAttribute("payment",orderInfo.getPayment());
        return "success";
    }



}
